package com.example.purchase.view.activity;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.purchase.R;
import com.example.purchase.bean.BaseBean;
import com.example.purchase.bean.DataBean;
import com.example.purchase.bean.MainBean;
import com.example.purchase.databinding.ActivityDetailsBinding;
import com.example.purchase.network.RetrofitClient;
import com.example.purchase.network.service.HttpService;
import com.example.purchase.view.base.BaseActivity;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.config.IndicatorConfig;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Predicate;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class DetailsActivity extends BaseActivity<ActivityDetailsBinding> implements View.OnClickListener {

    private List<Integer> mImage = new ArrayList<>();
    private String id;
    private final HttpService service = RetrofitClient.getInstance().getService(HttpService.class);

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_details;
    }

    @Override
    protected void initData() {
        mImage.add(R.mipmap.ic_xqt);
        dataBinding.banner.setAdapter(new BannerImageAdapter<Integer>(mImage) {
            @Override
            public void onBindView(BannerImageHolder holder, Integer data, int position, int size) {
                Glide.with(holder.itemView)
                        .load(data)
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .into(holder.imageView);
            }
        })
                .setIndicator(new CircleIndicator(this))
                .setIndicatorGravity(IndicatorConfig.Direction.CENTER);

    }

    @Override
    protected void initEmpty() {

    }

    @Override
    protected void initClick() {
        dataBinding.back.setOnClickListener(this);
        dataBinding.love.setOnClickListener(this);
    }

    @Override
    protected void initTimeOut() {
        id = getSharedPreferences("userMessage",Context.MODE_PRIVATE).getString("id","");
        HttpService service = RetrofitClient.getInstance().getService(HttpService.class);
        service.getTicketDetail(id,token).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .filter(new Predicate<BaseBean<MainBean>>() {
                    @Override
                    public boolean test(BaseBean<MainBean> mainBeanBaseBean) throws Throwable {
                        return String.valueOf(mainBeanBaseBean.getCode()).equals("200");
                    }
                }).subscribe(new Consumer<BaseBean<MainBean>>() {
            @Override
            public void accept(BaseBean<MainBean> mainBeanBaseBean) throws Throwable {
                if (mainBeanBaseBean.getData().isIs_like()){
                    dataBinding.love.setImageResource(R.mipmap.ic_xhx);
                    dataBinding.love.setClickable(false);
                }
                dataBinding.title.setText(String.valueOf(mainBeanBaseBean.getData().getTitle()));
                dataBinding.info.setText(String.valueOf(mainBeanBaseBean.getData().getInfo()));
                dataBinding.price.setText(String.valueOf(mainBeanBaseBean.getData().getPrice()));
                dataBinding.position.setText(String.valueOf(mainBeanBaseBean.getData().getCity()));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.love:
                dataBinding.love.setImageResource(R.mipmap.ic_xhx);
                Log.e("TAG", "onClick2: " + id);
                Log.e("TAG", "onClick3: " + token);
                service.like(id,token).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                        .filter(new Predicate<BaseBean<String>>() {
                            @Override
                            public boolean test(BaseBean<String> stringBaseBean) throws Throwable {
                                Log.e("TAG", "accept: " + stringBaseBean.getCode());
                                return String.valueOf(stringBaseBean.getCode()).equals("200");
                            }
                        }).subscribe(new Consumer<BaseBean<String>>() {
                    @Override
                    public void accept(BaseBean<String> stringBaseBean) throws Throwable {
                        Log.e("TAG", "accept: " + stringBaseBean);
                    }
                });
                dataBinding.love.setClickable(false);
                break;
        }
    }
}